package com.neuedu.it.mylink.service.impl;

import com.neuedu.it.mylink.dao.LinkDao;
import com.neuedu.it.mylink.dao.UserDao;
import com.neuedu.it.mylink.entity.Link;
import com.neuedu.it.mylink.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class LinkServiceImpl implements LinkService {
    @Autowired
    private LinkDao linkDao;
    @Autowired
    private UserDao userDao;

    @Override
    public Link getOne(int id) {
        return linkDao.findById(id).get();
    }

    @Override
    @Transactional
    public Link add(Link link) {
        linkDao.save(link);
        //递增该用户的联系人总数量
        //userDao.updateLinkNum(link.getUserId());
        return link;
    }
    @Override
    @Transactional
    public void delete(int id) {
       linkDao.deleteById(id);
    }

    @Override
    @Transactional
    public Link update(Link link) {
        linkDao.save(link);
        return link;
    }

    @Override
    public Page<Link> page(String findname, String findphone, int userId, Pageable pageable) {
        return linkDao.findByNameLikeAndPhoneLikeAndUserId(findname,findphone,userId,pageable);
    }
}
